﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Data;
using Manager;
namespace HealthSystem.sysAdmin
{
    public partial class ExportAdmin :BasePage
    {
        HW_Manger_Admin_UserManager manager = new HW_Manger_Admin_UserManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CustomerProfile.RoleId == 1001)
            {
                DataTable list = Hander();
                if (list != null)
                {
                    export_click(list);
                    //exportDataTable(list);
                }
            }
            else
            {
                Alert.Show("没有权限");
            }
        }
        private DataTable Hander()
        {
            string saction = Request["saction"];
            string susertype = Request["susertype"];
            string searchstring =HttpUtility.UrlDecode(Request["searchstring"]);
            DataTable list = null;

            int count = 0;
            switch (susertype)
            {
                case "a":
                    if (string.IsNullOrEmpty(saction))
                    {
                        list = manager.SearchRole(0, 10000, out count, 1002, null);
                    }
                    else
                    {
                        list = manager.SearchRole(0, 10000, out count, 1002, searchstring);
                    }
                    break;
                case "b":
                    if (string.IsNullOrEmpty(saction))
                    {
                        list = manager.SearchRole(0, 10000, out count, 1003, null);
                    }
                    else
                    {
                        list = manager.SearchRole(0, 10000, out count, 1003, searchstring);
                    }
                    break;
                case "c":
                    if (string.IsNullOrEmpty(saction))
                    {
                        list = manager.SearchRole(0, 10000, out count, 1011, null);
                    }
                    else
                    {
                        list = manager.SearchRole(0, 10000, out count, 1011, searchstring);
                    }
                    break;
                case "d":
                    if (string.IsNullOrEmpty(saction))
                    {
                        list = manager.SearchRole(0, 10000, out count, 1012, null);
                    }
                    else
                    {
                        list = manager.SearchRole(0, 10000, out count, 1012, searchstring);
                    }
                    break;
                default:
                    if (string.IsNullOrEmpty(saction))
                    {
                        list = manager.SearchRole(0, 10000, out count, 0, null);
                    }
                    else
                    {
                        list = manager.SearchRole(0, 10000, out count, 0, searchstring);
                    }
                    break;
            }
            return list;
        }   
        private void exportDataTable(DataTable dtData)
        {
            System.Web.UI.WebControls.DataGrid dgExport = null; 
            // 当前对话 
            System.Web.HttpContext curContext = System.Web.HttpContext.Current; 
            // IO用于导出并返回excel文件 
            System.IO.StringWriter strWriter = null; 
            System.Web.UI.HtmlTextWriter htmlWriter = null; 
 
            if (dtData != null) 
            { 
                // 设置编码和附件格式 
                curContext.Response.ContentType = "application/vnd.ms-excel"; 
                //curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
                curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                curContext.Response.Charset = "gb2312"; 
                 
                // 导出excel文件 
                strWriter = new System.IO.StringWriter(); 
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                dtData.Columns["XX_UserName"].ColumnName = "账号";
                // 为了解决dgData中可能进行了分页的情况，需要重新定义一个无分页的DataGrid 
                dgExport = new System.Web.UI.WebControls.DataGrid(); 
                dgExport.DataSource = dtData.DefaultView; 
                dgExport.AllowPaging = false; 
                dgExport.DataBind(); 
 
                // 返回客户端 
                dgExport.RenderControl(htmlWriter);     
                curContext.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>" +strWriter.ToString()); 
                curContext.Response.End(); 
            } 
        }
        private void export_click(DataTable list)
        {
         
            Grid Grid1 = new Grid();
            ExtAspNet.BoundField bf1=new ExtAspNet.BoundField();
            bf1.DataField="XX_UserName";
            bf1.HeaderText="账号";
            Grid1.Columns.Add(bf1);

            ExtAspNet.BoundField bf2 = new ExtAspNet.BoundField();
            bf2.DataField = "Name";
            bf2.HeaderText = "市区域";
            Grid1.Columns.Add(bf2);


            ExtAspNet.BoundField bf3 = new ExtAspNet.BoundField();
            bf3.DataField = "xx_password";
            bf3.HeaderText = "密码";
            Grid1.Columns.Add(bf3);


            ExtAspNet.BoundField bf4 = new ExtAspNet.BoundField();
            bf4.DataField = "WorkUnit";
            bf4.HeaderText = "工作单位";
            Grid1.Columns.Add(bf4);

            ExtAspNet.BoundField bf5 = new ExtAspNet.BoundField();
            bf5.DataField = "CNName";
            bf5.HeaderText = "联络员";
            Grid1.Columns.Add(bf5);

            ExtAspNet.BoundField bf6 = new ExtAspNet.BoundField();
            bf6.DataField = "OfficePhone";
            bf6.HeaderText = "联系电话";
            Grid1.Columns.Add(bf6);

            ExtAspNet.BoundField bf7 = new ExtAspNet.BoundField();
            bf7.DataField = "MobilePhone";
            bf7.HeaderText = "移动电话";
            Grid1.Columns.Add(bf7);

            Grid1.DataSource = list;
            Grid1.DataBind();
                    
            
            try
            {
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
                Response.ContentType = "application/excel";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.Charset = "gb2312"; 
                Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>"+GetGridTableHtml(Grid1));
                Response.Flush();
                Response.End();
                //HttpContext.Current.ApplicationInstance.CompleteRequest();
                //CreateExcel(list, "test.xls", "xls");
            }
            catch (Exception ex)
            {
                Alert.Show(ex.Message);
            }


            //Response.ClearContent();
            //Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
            //Response.ContentType = "application/excel";
            //Response.Write(GetGridTableHtml(Grid1));
            //Response.End();
        }
        private string GetGridTableHtml(Grid grid)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");

            sb.Append("<tr>");
            foreach (GridColumn column in grid.Columns)
            {
                sb.AppendFormat("<td>{0}</td>", column.HeaderText);
            }
            sb.Append("</tr>");


            foreach (GridRow row in grid.Rows)
            {
                sb.Append("<tr>");
                foreach (object value in row.Values)
                {
                    string html = value.ToString();
                    // 处理CheckBox
                    if (html.Contains("box-grid-static-checkbox"))
                    {
                        if (html.Contains("box-grid-static-checkbox-uncheck"))
                        {
                            html = "×";
                        }
                        else
                        {
                            html = "√";
                        }
                    }

                    // 处理图片
                    if (html.Contains("<img"))
                    {
                        string prefix = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "");
                        html = html.Replace("src=\"", "src=\"" + prefix);
                    }

                    sb.AppendFormat("<td>{0}</td>", html);
                }
                sb.Append("</tr>");
            }

            sb.Append("</table>");

            return sb.ToString();
        }
    }
}